const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// 使用CORS解决ajax跨域问题
app.use(function (request, response, next) {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.setHeader("Access-Control-Allow-Headers", "*");
  response.setHeader("Access-Control-Allow-Methods", "*");
  next();
});

app.all("/server", (req, res) => {
  res.send("这里是服务器");
});

app.all("/delay-server", (req, res) => {
  setTimeout(() => {
    console.log('服务器返回....')
    res.send("这里是延时服务器");
  }, 3000);
});

app.all("*", (req, res) => {
  if (req.method.toUpperCase() === "OPTIONS") {
    res.status(200).end("ok");
    return;
  }
  res.status(404);
  res.end("not found");
});

app.listen(8090, () => {
  console.log("服务已经启动....8090");
});
